<?php
/**
 * 会员管理
 * 
 * @author:  will.lee <im.will.lee@gmail.com>
 * @version: 1.0
 */
class MemberAction extends AdminAction
{
    /**
     * 会员列表
     */
     public function index()
     {
        if(IS_GET)
        {
            $this->data = array_map('urldecode',$_GET);
        }
        // 检索条件
        $where = $this->initSearchOption();
        $Member  = D('Member');
        $count   = $Member->where($where)->count();
        $p       = $this->page($count,$this->data);
        $members = $Member->field(true)->where($where)->order('id DESC')->limit($this->pageLimit)->select();
        $roles = D('Role')->getRoleList(true);
        $this->assign(compact('members','p','roles'));
        $this->display();
     }
     
     /**
      * 会员添加
      */
     public function add()
     {
        if(IS_AJAX)
        {
            $Member = D('Member');
            // 关闭批量验证
            $Member->setValidate('add');
            $Member->patchValidate = false;
            if($Member->create($this->data))
            {
                if($Member->add())
                {
                    $this->ajaxFormReturn('数据保存成功!',1);
                }
                else
                {
                    $this->ajaxFormReturn('系统发生错误');
                }
            }
            else
            {
                $this->ajaxFormReturn($Member->getError());
            }
        }
        $Role = D('Role');
        $this->assign('roles',$Role->getRoleList(true));
        $this->display();
     }
     
     /**
      * 会员编辑
      */
     public function edit()
     {
        $show_info = array();
        $Member = D('Member');
        if(IS_AJAX)
        {
            // 更新冲突检测
            $this->checkUpdateConflict($Member);
            // 关闭批量验证
            $Member->setValidate('save');
            $Member->patchValidate = false;
            // 是否更新密码
            if($this->data['password'] == '')
            {
                unset($this->data['password']);
            }
            else
            {
                $this->data['password'] = password_encode($this->data['password']);
            }
            if($data = $Member->create($this->data))
            {
                // 是否选择了文件
                if(!empty($_FILES))
                {
                    $upload_config  = C('Image.thumb');
                    $upload = $this->upload(1,$upload_config['save_path']);
                    if($upload['state'])
                    {
                        $data['thumb_icon'] = $upload['info'][0]['savename'];
                    }
                    else
                    {
                        $this->ajaxFormReturn($upload['message']);
                    }
                }
                
                if($Member->save($data))
                {
                    $this->ajaxFormReturn('数据保存成功!',1);
                }
                else
                {
                    $this->ajaxFormReturn('系统发生错误');
                }
            }
            else
            {
                $this->ajaxFormReturn($Member->getError());
            }
        }
        else
        {
            $uid = $this->_get('uid');
            if($uid == '')
            {
                $show_info['message'] = '参数错误!';
                $this->error($show_info);
            }
            $where  = array('id' => $uid);
            $result = $Member->field(true)->where($where)->find();
            if(empty($result))
            {
                $show_info['message'] = '数据不存在或已被删除!';
                $this->error($show_info);
            }
            $roles = D('Role')->getRoleList(true);
            $area  = D('Area')->getAreaDetail($result['area_id']);
            $this->assign(compact('result','roles','area'));
        }
        $this->display();
     }
     
     /**
      * 会员删除
      */
     public function delete()
     {
        if(IS_AJAX)
        {
            $uids = $this->_post('id');
            if(empty($uids))
            {
                $this->ajaxFormReturn('请至少选择一项!');
            }
            if(in_array($this->login_info['id'],$uids))
            {
                $this->ajaxFormReturn('不能删除当前操作者账号!');
            }
            $where  = array('id' => array('IN',$uids));
            $Member = D('Member');
            if($Member->where($where)->delete())
            {
                $this->ajaxFormReturn('删除成功!',1);
            }
            else
            {
                $this->ajaxFormReturn('系统发生错误');
            }
        }
     }
     
     /**
      * 禁止登陆
      */
     public function disable()
     {
        if(IS_AJAX)
        {
            $uids          = $this->_post('id');
            $active_status = $this->_get('state');
            if(empty($uids))
            {
                $this->ajaxFormReturn('请至少选择一项!');
            }
            if(in_array($this->login_info['id'],$uids))
            {
                $this->ajaxFormReturn('不能禁用当前操作者账号!');
            }
            $where  = array('id' => array('IN',$uids));
            $data   = array('active_status' => $active_status == 2 ? 2 :3);
            $Member = D('Member');
            if($Member->where($where)->save($data))
            {
                $this->ajaxFormReturn('更新成功!',1);
            }
            else
            {
                $this->ajaxFormReturn('系统发生错误');
            }
        }
     }
     
     /**
      * 检索条件组装
      */
     protected function initSearchOption()
     {
        $where = array();
        // 账号角色
        if(isset($this->data['role_id']) && $this->data['role_id'] != '')
        {
            $where['role_id'] = $this->data['role_id'];
        }
        // 账号区分
        if(isset($this->data['account_type']) && $this->data['account_type'] != '')
        {
            $where['account_type'] = $this->data['account_type'];
        }
        // 账号状态
        if(isset($this->data['active_status']) && $this->data['active_status'] != '')
        {
            $where['active_status'] = $this->data['active_status'];
        }
        // 注册日期(开始)
        if(isset($this->data['start_time']) && $this->data['start_time'] != '')
        {
            $where['created'] = array('EGT',$this->data['start_time']);
        }
        // 注册日期(结束)
        if(isset($this->data['end_time']) && $this->data['end_time'] != '')
        {
            if(isset($where['created']))
            {
                $where['created'] = array($where['created'],array('ELT',$this->data['end_time']),'AND');
            }
            else
            {
                $where['created'] = array('ELT',$this->data['end_time']);
            }
        }
        // 关键字
        if(isset($this->data['keyword']) && $this->data['keyword'] != '')
        {
            $or = array
            (
                'account'   => $this->data['keyword'],
                'nickname'  => $this->data['keyword'],
                'email'     => $this->data['keyword'],
                'mobile'    => $this->data['keyword'],
                '_logic'    => 'OR'
            );
            $where['_complex'] = $or;
        }
        return $where;
     }
}